package cn.njyd.corejava.thread.myday02;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class SellTicketThread implements Runnable{
    private int num = 50;

    private Lock lock = new ReentrantLock(true);

    @Override
    public void run() {
        while (true) {
            lock.lock();
            try{
                if(num<=0) {
                    System.out.println("票已卖完");
                    break;
                }
                num--;
                System.out.println(Thread.currentThread().getName()+"卖出一张票，目前还剩"+num+"张");
            }   finally {
                lock.unlock();
            }
        }
    }
}

public class LockDemo01 {
    public static void main(String[] args) {
        SellTicketThread sellTicketThread = new SellTicketThread();
        new Thread(sellTicketThread,"南京南站").start();
        new Thread(sellTicketThread,"南京北站").start();
        new Thread(sellTicketThread,"南京东站").start();




    }

}
